<?php
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: Shop.php 5341 2010-09-07 09:04:47Z huuphuoc $
 * @since		2.0.5
 */

class Shop_Models_Dao_Pdo_Mysql_Shop extends Tomato_Model_Dao
	implements Shop_Models_Interface_Shop
{
	public function convert($entity) 
	{
		return new Shop_Models_Shop($entity); 
	}
	public function getNextId(){		
		$config 					= Tomato_Config::getConfig(); 
		$prefix_code_shop			= $config->get('prefix_code')->shop_code;
		$prefix_code_shop_length 	= strlen($prefix_code_shop);
		$name_ini_length			= 'shop_code_length';
		$length_max_id				= $config->get('prefix_code')->$name_ini_length;
			
		$select = $this->_conn
					->select()
					->from(array('p' => $this->_prefix . 'shop'),'MAX(SUBSTR(p.id,'.($prefix_code_shop_length+1).')) as MAX_ID');
		$row = $select->query()
			   		  ->fetch();	
		$max_id =  (null == $row) ? 0 : $row->MAX_ID;
		$max_id++;
		$addition_zero_num 		= $length_max_id - strlen($max_id) - strlen($prefix_code_shop);
		
		$code = $prefix_code_shop;
		for($i=1;$i<=$addition_zero_num;$i++)
			$code.='0';
		$code.= $max_id;
		return $code;	
	}
	
	public function find($offset,$count,$exp=null){
		$select = $this->_conn
					->select()
					->from(array('s' => $this->_prefix . 'shop'));
		if ($exp) {
            if (isset($exp['shop_id'])) {
				$select->where("s.shop_id=?",$exp['shop_id']);
			}
		}		
        $select->order('s.id DESC');
        if(null != $count){
            $select->limit($count, $offset);
        }
        $rs = $select->query()
                     ->fetchAll();
        return new Tomato_Model_RecordSet($rs, $this);		
	}
	
	
	public 	function count($exp){
		$select = $this->_conn
                        ->select()
                        ->from(array('s' => $this->_prefix . 'shop'), array('num_shop' => 'COUNT(*)'));
		if ($exp) {
            if (isset($exp['shop_id'])) {
				$select->where("s.shop_id=?",$exp['shop_id']);
			}
		}
        return $select->query()->fetch()->num_shop;	
	}	
	
	public function getById($id) 
	{
		$row = $this->_conn
					->select()
					->from(array('s' => $this->_prefix . 'shop'))
					->where('s.id = ?', $id)
					->limit(1)
					->query()
					->fetch();
		return (null == $row) ? null : new Shop_Models_Shop($row);
	}

	public function add($shop) 
	{
			$data = array(
						"id"				=> $shop->id,
						"shop_type"			=> $shop->shop_type,
						"shop_name_vi_VN"	=> $shop->shop_name_vi_VN,
						"adress"			=> $shop->adress,
						"tel"				=> $shop->tel,
						"fax"				=> $shop->fax,
						"currency"			=> $shop->currency,
						"parent_id"			=> $shop->parent_id,
						"tax_code"			=> $shop->tax_code,
						"introduction_vi_VN"=> $shop->introduction_vi_VN,
						"introduction_en_US"=> $shop->introduction_en_US,
						"rate"				=> $shop->rate,	
						"shop_name_en_US"	=> $shop->shop_name_en_US,							
			);
			
			if (isset($shop->id) && $shop->id != null) {
				$data['id'] = $shop->id;
				$this->_conn->insert($this->_prefix . 'shop', $data);
				return $shop->id;				
			} else {
				$this->_conn->insert($this->_prefix . 'shop', $data);
				return $this->_conn->lastInsertId($this->_prefix . 'shop');
			}
	}
	
	public function update($shop) 
	{
		$this->_conn->update($this->_prefix . 'shop', 
							array(
									"shop_type"			=> $shop->shop_type,
									"shop_name_vi_VN"	=> $shop->shop_name_vi_VN,
									"adress"			=> $shop->adress,
									"tel"				=> $shop->tel,
									"fax"				=> $shop->fax,
									"currency"			=> $shop->currency,
									"parent_id"			=> $shop->parent_id,
									"tax_code"			=> $shop->tax_code,
									"introduction_vi_VN"=> $shop->introduction_vi_VN,
									"introduction_en_US"=> $shop->introduction_en_US,
									"rate"				=> $shop->rate,	
									"shop_name_en_US"	=> $shop->shop_name_en_US,							
							), 
							array(
								'id = ?' => $shop->id,
							));
	}
	
	public function delete($shop) 
	{
		$this->_conn->delete($this->_prefix . 'shop', 
							array(
								'id = ?' => $shop->id,
							));
	}

	/* Lấy spa  cha của bảng có prefix*/
	public function getParentShop(){
		$row = $this->_conn
					->select()
					->from(array('s' => $this->_prefix . 'shop'))
					->where('s.parent_id IS NULL')
					->limit(1)					
					->query()
					->fetch();	
		if($row)
			return new Shop_Models_Shop($row);
		else return null;	
	}
}